package com.gomoku.backend.service.impl.user;

import com.gomoku.backend.pojo.User;
import com.gomoku.backend.service.impl.utils.UserDetailsImpl;
import com.gomoku.backend.service.user.InfoService;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class InfoServiceImpl implements InfoService {

    @Override
    public Map<String, String> getinfo() {
        //登录之后UsernamePasswordAuthenticationToken就是通过用户名密码得到的details上下文，getUser得到登录用户
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl userDetails = (UserDetailsImpl) token.getPrincipal();
        User loginUser = userDetails.getUser(); //通过上下文的jwt解析出sub:“id值”,得到的user就是登录用户

        Map<String,String> map = new HashMap<>();
        map.put("error_message","success");
        map.put("id",loginUser.getId().toString());
        map.put("username",loginUser.getUsername());
        map.put("photo",loginUser.getPhoto());
        return map;
    }
}
